package com.kfm.goods.mapper;

import com.kfm.goods.domain.User;
import com.kfm.goods.domain.UserDTO;
import com.kfm.goods.provider.UserSQLProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author 10854
* @description 针对表【user】的数据库操作Mapper
* @createDate 2023-11-06 09:57:25
* @Entity com.kfm.goods.domain.User
*/
@CacheNamespace(size = 512)
public interface UserMapper {

    int deleteByPrimaryKey(String id);

    @InsertProvider(type = UserSQLProvider.class, method = "insertSelective")
    int insertSelective(User record);

    User selectByPrimaryKey(String id);

    @UpdateProvider(type = UserSQLProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(User record);

    @Select({
            "<script>",
            """
            select
                    id,username,password,
                    name,phone,email,
                    avatar,state,create_date,
                    update_date, dept_id
                from user
                <where>
                    <if test="id!= null">
                        and id = #{id}
                    </if>
                    <if test="username!= null and username != ''">
                        and username = #{username}
                    </if>
                    <if test="password!= null">
                        and password = #{password}
                    </if>
                    <if test="name!= null and name != ''">
                        and name = #{name}
                    </if>
                    <if test="phone!= null">
                        and phone = #{phone}
                    </if>
                    <if test="email!= null">
                        and email = #{email}
                    </if>
                    <if test="avatar!= null">
                        and avatar = #{avatar}
                    </if>
                    <if test="state!= null">
                        and state = #{state}
                    </if>
                    <if test="createDate!= null">
                        and create_date = #{createDate}
                    </if>
                    <if test="updateDate!= null">
                        and update_date = #{updateDate}
                    </if>
                </where>
        """,
            "</script>"
    })
    List<User> selectAll(User user);


    @SelectProvider(type = UserSQLProvider.class, method = "selectAll2")
    List<User> selectAll2(User user);


    @Select("select t1.*, t2.dept_name, t2.descr from user t1 left join dept t2 on t1.dept_id = t2.id")
    @Results(id = "userResultWithDept", value = {
            @Result(property = "id", column = "id", id = true),
            // 省略其他字段
            @Result(property = "dept", one = @One(resultMap = "com.kfm.goods.mapper.DeptMapper.deptResult")),
    })
    List<UserDTO> selectWithDept(User user);


    @Select("select * from user")
    @Results(id = "userResultWithDept2", value = {
            @Result(property = "id", column = "id", id = true),
            // 省略其他字段
            @Result(property = "dept", column = "dept_id", one = @One(select = "com.kfm.goods.mapper.DeptMapper.selectByPrimaryKey")),
    })
    List<UserDTO> selectWithDept2(User user);

    List<User> selectByDeptId(Integer deptId);

    @Select("select t1.*, t3.id role_id, t3.role_name, t3.role_desc from user t1\n" +
            "            left join user_role t2 on t1.id = t2.user_id\n" +
            "            left join role t3 on t2.role_id = t3.id")
    @Results(id = "userResultWithRoles", value = {
            @Result(property = "id", column = "id", id = true),
            // 省略其他字段
            @Result(property = "roles", many = @Many(resultMap = "com.kfm.goods.mapper.RoleMapper.roleMap")),
    })
    List<UserDTO> selectAllWithRoles();
}
